package xyz.zhangyi.onjava.java8.example.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class Operators {
    public static void main(String[] args) {
        filterAndDistinct();
        Formatter.printDottedLine();
        useMap();
        Formatter.printDottedLine();
        useFlatmap();
    }

    public static void filterAndDistinct() {
        List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
        numbers.stream()
                .filter(i -> i % 2 == 0)
                .distinct()
                .forEach(System.out::println);

        // more operators: limit(n)  skip(n)
    }

    public static void useMap() {
        List<String> words = Arrays.asList("Hello", "Java8");
        words.stream()
                .map(word -> word.split(""))
                .map(letterArr -> letterArr.length)
                .forEach(System.out::println);
    }

    public static void useFlatmap() {
        List<String> words = Arrays.asList("Hello", "Java8");
//        words.stream()
//                .map(word -> word.split(""))
//                .flatMap(Arrays::stream)
//                .distinct()
//                .forEach(System.out::println);
//
        words.stream()
                .flatMap(word -> Stream.of(word.split("")))
                .distinct()
                .forEach(System.out::println);

    }

}
